package com.ohuang.wallpapermanager.screen

import android.app.Activity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.ohuang.wallpapermanager.WallPaperServiceManager
import com.ohuang.wallpapermanager.compose.clickableSafe
import com.ohuang.wallpapermanager.compose.dp2sp
import com.ohuang.wallpapermanager.compose.getContext
import com.ohuang.wallpapermanager.config.LocalConfig
import com.ohuang.wallpapermanager.config.LogConfig
import com.ohuang.wallpapermanager.util.AppUtil
import com.ohuang.wallpapermanager.util.TimeUtil
import com.ohuang.wallpapermanager.wallpaper.CarouselWallPaperDispatch
import com.ohuang.wallpapermanager.workmanager.CarouseWallPaperWorkManager

@Preview(backgroundColor = 0xffffffff, showBackground = true)
@Composable
private fun HomeScreenShow() {
    HomeScreen(rememberNavController = rememberNavController())
}

@Composable
fun HomeScreen(rememberNavController: NavHostController) {


    Column(modifier = Modifier.fillMaxWidth()) {
        TopTitleBar(title = "首页")
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .verticalScroll(rememberScrollState())
                .padding(15.dp)
        ) {
            HomeActionItemComposable(title = "设置锁屏壁纸") {
                rememberNavController.navigate(Destination.settingWallPaper(true)) {}

            }
            HomeActionItemComposable(title = "设置桌面壁纸") {
                rememberNavController.navigate(Destination.settingWallPaper(false)) {}
            }
            HomeActionItemComposable(title = "设置定时锁屏壁纸") {
                rememberNavController.navigate(Destination.wallPaperList(true)) {}
            }

            HomeActionItemComposable(title = "设置定时桌面壁纸") {
                rememberNavController.navigate(Destination.wallPaperList(false)) {}
            }
            val context = getContext()
            HomeActionItemComposable(title = "启动动态壁纸(可能需要手动开启权限)") {
                WallPaperServiceManager.start(context as Activity)
            }
            HomeActionItemComposable(title = "其他设置") {
                rememberNavController.navigate(Destination.SETTINGS) {}
            }

        }

    }

}


@Composable
fun HomeActionItemComposable(title: String, action: () -> Unit) {
    Row(modifier = Modifier
        .fillMaxWidth()
        .height(50.dp)
        .clickableSafe {
            action()
        }) {
        Text(
            modifier = Modifier.align(Alignment.CenterVertically),
            text = title,
            fontSize = 16.dp2sp()
        )
    }
}

